package com.common.www.bean.entity.recycle;

//import com.bianmaba.realestate.bean.entity.authorization.UserRoleMiddle;

import com.common.www.MyApplicationListener;
import org.springframework.boot.base.jpa.domain.annotations.QueryCacheable;
import org.springframework.boot.base.jpa.domain.entity.NumberIdentityEntity;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import java.math.BigDecimal;
import java.math.RoundingMode;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * 卖货点工厂
 *
 * @author luozisong
 */
@DynamicUpdate(true)
@DynamicInsert(true)
@Entity
@Getter
@Setter
@QueryCacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Table()
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "project返回参数")
public class RecycleTypePrice extends NumberIdentityEntity {
    private static final long serialVersionUID = -2278084748058910042L;

    @JsonIgnoreProperties(value = {"parent", "children"})
    @ManyToOne()
    @JoinColumn(name = "saleType")
    private RecycleType recycleType;

    /**
     * 省份
     */
    @Column(length = 32)
    @ApiModelProperty(value = "省份", example = "四川省")
    private String province;
    /**
     * 城市
     */
    @Column(length = 32)
    @ApiModelProperty(value = "市", example = "成都市")
    private String city;
    /**
     * 城市编码
     */
    @Column(length = 12)
    private String code;

    /**
     * 最低单价 元/kg 或元/吨 等，根据物品的类型来定
     */
    @Column(precision = 23, scale = 2)
    private BigDecimal minPrice;
    /**
     * 最高单价 元/kg 或元/吨 等，根据物品的类型来定
     */
    @Column(precision = 23, scale = 2)
    private BigDecimal maxPrice;
    /**
     * 建议单价 元/kg 或元/吨 等，根据物品的类型来定
     */
    @Column(precision = 23, scale = 2)
    private BigDecimal suggestPrice;
    /**
     * 单位类型 1.元/吨 2.元/KG 3.个
     */
    @Column(length = 11)
    private Integer unitType;


    /**
     * 核价建议
     */
    @Column(length = 1024)
    private String remark;
    /**
     * 配图的OSS对象名称
     */
    @Column(length = 128)
    private String img;

    @Transient
    private BigDecimal minPriceKg;
    @Transient
    private BigDecimal maxPriceKg;
    @Transient
    private BigDecimal suggestPriceKg;
    @Transient
    private String unitTypeName;


    public String getDownloadUrl() {
        return MyApplicationListener.publicProjectPath + img;
    }

    /**
     * 得到kg的单价
     */
    public BigDecimal getMinPriceKg() {
        if (unitType == null) {
            return BigDecimal.ZERO;
        }
        if (unitType == 1) {
            if (minPrice != null) {
                minPriceKg = minPrice.divide(BigDecimal.valueOf(1000), 2, RoundingMode.HALF_UP);
            }
        } else if (unitType == 2) {
            return minPriceKg;
        } else if (unitType == 3) {
            return minPriceKg;
        }
        return minPriceKg;
    }


    /**
     * 得到kg的单价
     */
    public BigDecimal getMaxPriceKg() {
        if (unitType == null) {
            return BigDecimal.ZERO;
        }
        if (unitType == 1) {
            if (maxPriceKg != null) {
                maxPriceKg = maxPrice.divide(BigDecimal.valueOf(1000), 2, RoundingMode.HALF_UP);
            }
        } else if (unitType == 2) {
            return maxPriceKg;
        } else if (unitType == 3) {
            return maxPriceKg;
        }

        return maxPriceKg;
    }

    public BigDecimal getSuggestPriceKg() {
        if (unitType == null) {
            return BigDecimal.ZERO;
        }
        if (unitType == 1) {
            if (suggestPriceKg != null) {
                suggestPriceKg = suggestPrice.divide(BigDecimal.valueOf(1000), 2, RoundingMode.HALF_UP);
            }
        } else if (unitType == 2) {
            return suggestPriceKg;
        } else if (unitType == 3) {
            return suggestPriceKg;
        }

        return suggestPriceKg;
    }


    public String getUnitTypeName() {
        if (unitType == null) {
            return "未知";
        }
        if (unitType == 1) {
            unitTypeName = "元/吨";
        } else if (unitType == 2) {
            unitTypeName = "元/KG";
        } else if (unitType == 3) {
            unitTypeName = "元/个";
        }
        return unitTypeName;
    }


}